package com.example.blogsystem.utils;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.io.Decoders;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;
import java.security.Key;
import java.util.HashMap;
import java.util.Map;
@Slf4j
public class JwtUtils {
    private static long expiration = 30*60*1000;//30分钟后过期
    private static String secreString="/+A4ORppH1UzQVulxtH0Y3+Mlk8yQGcEhg4dsn8CcUc=";
    private static Key key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secreString));
    /*private static Key key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secreString));*/
    /*
    * 生产令牌
    * */
    public static String genJWT(Map<String,Object> claims) {
       return Jwts.builder()
                .setClaims(claims)
               .signWith(key)
                .compact();
    }
    /*
    * 校验令牌
    * *//*setSigningKey(key).bulid();*/
    public static Claims verify(String token){
        JwtParser build = Jwts.parser().setSigningKey(key);
        Claims claims = null;
        try{
            claims = build.parseClaimsJws(token).getBody();
        }
        catch (Exception e){
            log.error("token校验失败,token"+token);
        }
        return claims;
    }
    public static Integer getUserInfoToken(String token) {
        Claims claims = verify(token);
        Assert.notNull(claims,"token不合法");
        Integer userId = (Integer) claims.get("id");
       return userId;
    }
}
